package model;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;


public class World {
	
	//level
	Level level;
	// the block making up the world
	Array<Block> blocks = new Array<Block>();
	//colistion rect
	Array<Rectangle> collistionRects = new Array<Rectangle>();
	// the hero
	Bob bob;
	//get block //get hero
	///
	Array<Bullet> fires = new Array<Bullet>();
	//fire of bob
	//door: item special
	Door door;
	//music
	public Array<Rectangle> getCollistionRects(){
		return collistionRects;
	}
	public Bob getBob(){
		return bob;
	}
	public Level getLevel(){
		return level;
	}
	public Array<Bullet> getFire(){
		return bob.getFire();
	}
	public Vector2 getPositionDoor(){
		return door.getPosition();
	}
	public Door getDoor(){
		return door;
	}
	public List<Block> getDrawableBlocks(int width, int height){
		int x = (int )bob.getPosition().x - width;
		int y=(int )bob.getPosition().y-height;
		if(x<0)
			x=0;
		if(y<0)
			y=0;
		int x2= x+2*width;
		int y2= y +2*height;
		if(x2>=level.getWidth())
			x2=level.getWidth()-1;
		if(y2>=level.getHeight())
			y2=level.getHeight()-1;
		List <Block> blocks = new ArrayList<Block>();
		Block block;
		try{
		for(int col=x;col<=x2;col++)
		{
			for(int row=y;row<=y2;row++){
				block = level.getBlock()[col][row];
				if(block!=null)
					blocks.add(block);
				
			}
		}
		}
		catch(Exception e){
			System.out.print("dfs");
		}
		return blocks;
	}
	
	//-----------------------
	
	public World(){
		//createDemoWorld();
		createWorld(1);
	}
	private void createDemoWorld (){
		bob= new Bob (new Vector2(2,4));
		level.setLevel(0);
		
	}
	private void createWorld(int lev){
		level=new Level();
		level.setLevel(lev);
		bob = new Bob (new Vector2(level.getPositionBob().x,level.getPositionBob().y));
		door = new Door(new Vector2(level.getPositionDoor().x,level.getPositionDoor().y));
	}
	public boolean nextLevel(){
		blocks.clear();
		boolean kq=level.nextLevel();
		bob.setPosition(level.getPositionBob());
		door = new Door(new Vector2(level.getPositionDoor().x,level.getPositionDoor().y));
		return kq;
	}
}
